
from fastapi import Request, Form
from src.core.session import *
from src.entity.models import Bug, UpdateBug, CreateBug
from src.core.logconfig import log
from src.common.response import success_response, fail_response
from src.utils.tools import get_cur_time
from src.common.base_router import BaseRouter
from src.common.base_service import BaseService

log = log.logger
sc = SQLCON

bug = BaseRouter(
    entity=Bug,
    create_schema=CreateBug,
    update_schema=UpdateBug,
    prefix="/sys/bug",
    tags=["Bug"]
).router

bug_service = BaseService(Bug)


async def query_all():
    return bug_service.get_list()


async def query_one(id):
    return bug_service.get_by_id(id)

async def create_bug(
    title: str = Form(..., description="Bug标题"), 
    content1: str = Form(default="1. xxx\n2. xxx\n3. xxx\n", description="执行步骤"), 
    product: str = Form(default="测试", description="产品名称"), 
    discovery_env: str = Form(default="测试环境", description="发现环境"),
    platform: str = Form(default="", description="平台"), 
    device_type: str = Form(default="", description="设备类型"), 
    content2: str = Form(default="", description="发现描述"),
    state: int = Form(default="0", description="状态"),
    resolve_desc: str = Form(default="", description="解决描述"),
    start_time: str = Form(default=get_cur_time(), description="开始时间"),
    end_time: str = Form(default=get_cur_time(), description="结束时间"),
    developer: str = Form(default="", description="开发人员"),
    tester: str = Form(default="", description="测试人员"),
    link_req: str = Form(default="", description="关联需求"),
    rank: str = Form(default="", description="优先级"),
    level: str = Form(default="", description="问题等级"),
    discovery_version: str = Form(default="", description="发现版本"),
    operator: str = Form(default="", description="操作人"),
    ):
    
    create_data = CreateBug(
        title=title, 
        content1=content1, 
        product=product, 
        discovery_env=discovery_env, 
        platform=platform, 
        device_type=device_type, 
        state=state, 
        content2=content2, 
        resolve_desc=resolve_desc, 
        start_time=start_time, 
        end_time=end_time, 
        developer=developer, 
        tester=tester, 
        link_req=link_req, 
        rank=rank, level=level, 
        discovery_version=discovery_version,
        create_time = get_cur_time(),
        operator=operator) 
    
    return bug_service.create(create_data)

async def update_bug(
    id: int = Form(..., description="Bug id"),
    title: str = Form(..., description="Bug标题"), 
    content1: str = Form(default="1. xxx\n2. xxx\n3. xxx\n", description="执行步骤"), 
    product: str = Form(default="测试", description="产品名称"), 
    discovery_env: str = Form(default="测试环境", description="发现环境"),
    platform: str = Form(default="", description="平台"), 
    device_type: str = Form(default="", description="设备类型"), 
    state: int = Form(default="0", description="状态"),
    content2: str = Form(default="", description="解决描述"),
    start_time: str = Form(default=get_cur_time(), description="开始时间"),
    end_time: str = Form(default=get_cur_time(), description="结束时间"),
    developer: str = Form(..., description="开发人员"),
    tester: str = Form(..., description="测试人员"),
    link_req: str = Form(default="", description="关联需求"),
    rank: str = Form(default="", description="优先级"),
    level: str = Form(default="", description="问题等级"),
    discovery_version: str = Form(default="", description="发现版本"),
    update_time: str = Form(default=get_cur_time(), description="更新时间"),
    operator: str = Form(default="", description="操作人"),
    ):
    
    update_data = UpdateBug(
        id=id,
        title=title, 
        content1=content1, 
        product=product, 
        discovery_env=discovery_env, 
        platform=platform, 
        device_type=device_type, 
        state=state, 
        content2=content2, 
        start_time=start_time, 
        end_time=end_time, 
        developer=developer, 
        tester=tester, 
        link_req=link_req,
        rank=rank, level=level, 
        discovery_version=discovery_version,
        update_time = update_time,
        operator=operator) 
    return bug_service.update(update_data)
